<?php


namespace app\sys\service;


use app\admin\model\Reply;
use app\admin\model\Staff;
use app\common\CommandCron;
use GatewayWorker\Lib\Gateway;
use think\facade\Log;
use think\facade\Request;
use think\worker\Events;
use Workerman\Lib\Timer;
use Workerman\Worker;
use Workerman\Crontab\Crontab;

class GateEvents extends Events
{
    /**
     * onWorkerStart 事件回调
     * 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
     *
     * @access public
     * @param  \Workerman\Worker    $businessWorker
     * @return void
     */
    public static function onWorkerStart(Worker $businessWorker)
    {
    }

    /**
     * onConnect 事件回调
     * 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
     *
     * @access public
     * @param int $client_id
     * @return void
     */
    public static function onConnect($client_id)
    {
        Gateway::sendToClient($client_id, json_encode(array(
            'type' => 'init',
            'client_id' => $client_id,
        )));
    }

    /**
     * onMessage 事件回调
     * 当客户端发来数据(Gateway进程收到数据)后触发
     *
     * @access public
     * @param  int       $client_id
     * @param  mixed     $data
     * @return void
     */
    public static function onMessage($client_id, $data)
    {
        $req_data = json_decode($data, true);
        Log::info("心跳".$data);
        if(isset($req_data['type']) && $req_data['type'] == 'ping' ){
            $send_data = json_encode(['type'=>'pong','status'=> 1,'message'=> '心跳']);
            Gateway::sendToClient($client_id,$send_data);
        }
    }


    /**
     * onClose 事件回调 当用户断开连接时触发的方法
     *
     * @param integer $client_id 断开连接的客户端client_id
     * @return void
     */
    public static function onClose($client_id)
    {
        // 定时器修改客服状态
        // 离线触发
        Log::info("onClose断开操作:");
        $cli = new GateCli();
        list($useType,$uid,$staffNo,$userNo,$id,$isMen) = $cli->getUidByClientIdAndType($client_id);

        if ($useType == "err"){
            Log::info("onClose断开操作：找不到在线_{$client_id} - $uid - $staffNo - $userNo - $useType");
        }


        // 系统操作
        if ($useType == GateCli::$staPre){
            $data = array(
                'type' => 'staff_close',
                'client_id' => $client_id,
                'user_no' => $uid,
                'user_id'=>$id,
                "user_type"=>$isMen
            );
            $mess = wsJson("success", $data);
            // 客服离线
            $err = $cli->staffOut($client_id,$uid,$mess);
            Log::info("onClose断开操作：{$client_id}_{$err}");
        }else if ($useType == GateCli::$usePre){
            $data = array(
                'type' => 'user_close',
                'client_id' => $client_id,
                'user_no' => $uid,
                'user_id'=>$id,
                "user_type"=>$isMen
            );
            $mess = wsJson("success", $data);
            // 客户离线
            $err = $cli->userOut($client_id,$uid,$mess);
            Log::info("onClose断开操作：{$client_id}_{$err}");
        }
        Log::info("onClose断开操作：找不到在线_{$client_id}-$uid - $staffNo - $userNo");

        // 列表进出
        staff::allocStaff("",$uid,"af");
    }

    /**
     * onWorkerStop 事件回调
     * 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
     *
     * @param  \Workerman\Worker    $businessWorker
     * @return void
     */
    public static function onWorkerStop(Worker $businessWorker)
    {
//        Timer::del($businessWorker->TimerId);
//        $businessWorker->CrontabId
        echo "WorkerStop\n";
    }
}